package sartoservicesync;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sartoservicesync.connectors.PrestaConnector;
import sartoservicesync.connectors.RakutenConnector;
import sartoservicesync.entities.presta.PrestaCategory;
import sartoservicesync.entities.rakuten.RakutenCategory;

public class SartoService {
	static final Logger logger = LoggerFactory.getLogger(SartoService.class);
	
	public static void SyncCategoriesPrestaToRakuten() {
		logger.debug("Synchronisiere Kategorien zwischen PrestaShop und Rakuten");
		PrestaConnector presta = new PrestaConnector();
		RakutenConnector rakuten = new RakutenConnector();
		List<PrestaCategory> prestaCategories = presta.loadFullCategoriesList();
		List<RakutenCategory> rakutenCategories = rakuten.loadFullCategoriesList();
		logger.trace("Anzahl an Kategorien: Prestashop - {}, Rakuten - {}", prestaCategories.size(), rakutenCategories.size());
		
		List<RakutenCategory> missingInRakuten = new ArrayList<RakutenCategory>();
		for(PrestaCategory presCat : prestaCategories){
			boolean missing = true;
			for(RakutenCategory rakCat:rakutenCategories){
				if(rakCat.getName().equals(presCat.getName())){
					missing=false;
					break;
				}
			}
			if(missing){
				RakutenCategory newCat = new RakutenCategory();
				newCat.setName(presCat.getName());
				missingInRakuten.add(newCat);
			}
		}
		
		logger.debug("Anzahl fehlender Kategorien in Rakuten: {}",missingInRakuten.size());
		if(logger.isTraceEnabled()){
			logger.trace("Fehlende Kategorien:");
			for(RakutenCategory cat:missingInRakuten){
				logger.trace(cat.getName());
			}
		}
		
		rakuten.createCategories(missingInRakuten);
	}
}
